package com.e3.sysmg.security.server;

import java.util.List;

import com.e3.core.base.ServiceException;
import com.e3.core.base.ServiceLocator;
import com.e3.sysmg.security.client.UserRPCService;
import com.e3.sysmg.security.model.UserModel;
import com.e3.sysmg.security.model.UserQC;
import com.e3.sysmg.security.service.SecurityQueryService;
import com.e3.sysmg.security.service.SecurityService;
import com.e3.web.core.client.WebException;
import com.e3.web.core.server.GwtRPCAction;
import com.e3.web.core.server.GwtRPCExecutor;
import com.e3.web.core.server.GwtRPCVoidExecutor;

public class UserRPCServiceImpl extends GwtRPCAction implements UserRPCService {
	
	private SecurityService secService;
	private SecurityQueryService secQueryService;
	
	private SecurityService getSecService() {
		if(secService == null){
			secService = ServiceLocator.getService(SecurityService.class);
		}
		return secService;
	}

	private SecurityQueryService getSecQueryService() {
		if(secQueryService == null){
			secQueryService = ServiceLocator.getQueryService(SecurityQueryService.class);
		}
		return secQueryService;
	}

	public UserModel newUser(final UserModel userModel) throws WebException {
		return new GwtRPCExecutor<UserModel>(){
			@Override
			public UserModel execute() throws ServiceException, Exception {
				return getSecService().newUser(userModel);
			}
			
		}.doExecute();
	}

	public UserModel modifyUser(final UserModel userModel) throws WebException {
		return new GwtRPCExecutor<UserModel>(){
			@Override
			public UserModel execute() throws ServiceException, Exception {
				return getSecService().modifyUser(userModel);
			}
			
		}.doExecute();
	}

	public void deleterUsers(final List<UserModel> userModels) throws WebException {
		new GwtRPCVoidExecutor() {
			@Override
			public void execute() throws ServiceException, Exception {
				getSecService().deleteUsers(userModels);
			}
		};
	}

	public List<UserModel> getUserByCondition(final UserQC qc) throws WebException {
		return new GwtRPCExecutor<List<UserModel>>(){
			@Override
			public List<UserModel> execute() throws ServiceException, Exception {
				return getSecQueryService().getUserByCondition(qc);
			}
		}.doExecute();
	}
}
